
#include "setting_update.h"
#include "sys_font.h"
#include "pt_fs.h"

static ui_setting_update_t  ui_set_update;


static  char * icon_esc[1] = 
{
    "Return.png",
};

static int setting_update_clicked_event(void)
{
    page_install("setting_download", lv_page_setting_download_create(),NULL);
    page_push_anim("setting_download",LOAD_ANIM_MOVE_LEFT);  

    return 0;
}

static void setting_update_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("update clicked\n");
        setting_update_clicked_event();
    }
}

static int setting_update_esc_clicked(lv_event_t* event)
{
    page_pop();
    root_gesture_enable();

    return 0;
}

static void setting_update_esc_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("esc\n");
        setting_update_esc_clicked(event);
    }
}

int ui_setting_update_create(lv_obj_t *superview)
{
    char txt_buff[256];

    ui_set_update.txt_show = malloc(4096);
    if(!ui_set_update.txt_show){
        debug_e("malloc err\n");
        return -1;
    }
    
    ui_set_update.img_bg = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_update.img_bg,16,8);
    lv_obj_set_size(ui_set_update.img_bg,32,32);

	lv_obj_set_style_radius(ui_set_update.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_update.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_update.img_bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_update.img_bg, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_update.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_update.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_update.img_bg, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_update.img_bg, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_set_update.img_bg,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_update.img_bg,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_update.img_bg,lv_color_make(0xff,0,0),0);
    #endif 
    //************************
    ui_set_update.img_esc = lv_img_create(ui_set_update.img_bg);
    lv_img_set_src(ui_set_update.img_esc,pt_fs_get(icon_esc[0]));
    lv_obj_set_pos(ui_set_update.img_esc,0,0);

    lv_obj_add_event_cb(ui_set_update.img_bg,setting_update_esc_event_callback,LV_EVENT_ALL,NULL);
	
    //**********************************************************************************************
    ui_set_update.title = lv_label_create(superview);
    lv_obj_set_pos(ui_set_update.title,102,6);
    lv_obj_set_size(ui_set_update.title,276,25);
    lv_obj_set_style_text_align(ui_set_update.title,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_update.title,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_update.title,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_update.title, lv_font_get(18), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_update.title, "固件更新");
    //**********************************************************************************************

    ui_set_update.version = lv_label_create(superview);
    lv_obj_set_pos(ui_set_update.version,21,52);
    lv_obj_set_size(ui_set_update.version,241,25);
    lv_obj_set_style_text_align(ui_set_update.version,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui_set_update.version,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_update.version,lv_color_make(0x99,0x99,0x99),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_update.version, lv_font_get(18), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,256,"版本%s更新内容：","v0.1");

    lv_label_set_text( ui_set_update.version, txt_buff);
    //**********************************************************************************************
    //soft version info

    ui_set_update.bg_txt = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_update.bg_txt,21,87);
    lv_obj_set_size(ui_set_update.bg_txt,437,144);

	lv_obj_set_style_radius(ui_set_update.bg_txt, 5, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_update.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_update.bg_txt, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_update.bg_txt, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_update.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_update.bg_txt, 0, LV_PART_MAIN);
	//lv_obj_set_scrollbar_mode(ui_set_update.bg_txt, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_update.bg_txt, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_set_update.bg_txt,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_update.bg_txt,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_set_update.bg_txt,lv_color_make(0,0,0),0);
    #endif 

    //txt

    ui_set_update.txt = lv_label_create(ui_set_update.bg_txt);
    lv_obj_set_pos(ui_set_update.txt,0,0);
    //lv_obj_set_size(ui_set_update.txt,241,25);
    lv_obj_set_style_text_align(ui_set_update.txt,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui_set_update.txt,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_update.txt,lv_color_make(0x99,0x99,0x99),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_update.txt, lv_font_get(18), LV_STATE_DEFAULT);

    lv_obj_set_width(ui_set_update.txt, 420);
    lv_label_set_long_mode(ui_set_update.txt, LV_LABEL_LONG_WRAP);

    #if 0
    lv_obj_set_style_bg_opa(ui_set_update.txt,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_update.txt,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_set_update.txt,lv_color_make(0,0,0),0);
    #endif 

    memset(ui_set_update.txt_show,0,4096);
    snprintf(ui_set_update.txt_show,4096,"版本%s更新内容多久哦睡觉翻身解放拉萨附近数量大幅减少的房间数量大幅减少咖啡碱撒法地方\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    对方手里发动就爱上了对方就阿斯弗都放假撒两地分居萨拉发达省份的的肌肤拉法兰快递费啊打飞机啊劳动法发\
    ","v0.1");

    lv_label_set_text(ui_set_update.txt, ui_set_update.txt_show);
    //**********************************************************************************************
    //update btn

    ui_set_update.bg_update = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_update.bg_update,65,250);
    lv_obj_set_size(ui_set_update.bg_update,350,50);

	lv_obj_set_style_radius(ui_set_update.bg_update, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_update.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_update.bg_update, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_update.bg_update, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_update.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_update.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_update.bg_update, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_update.bg_update, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui_set_update.bg_update,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_update.bg_update,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_update.bg_update,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(ui_set_update.bg_update, setting_update_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_set_update.txt_update = lv_label_create(superview);
    lv_obj_set_pos(ui_set_update.txt_update,115,260);
    lv_obj_set_size(ui_set_update.txt_update,250,30);
    lv_obj_set_style_text_align(ui_set_update.txt_update,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_update.txt_update,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_update.txt_update,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_update.txt_update, lv_font_get(18), LV_STATE_DEFAULT);

    lv_label_set_text(ui_set_update.txt_update,"立即更新");
    //**********************************************************************************************

    return 0;
}

int ui_setting_update_destroy(lv_obj_t *superview)
{

    if(ui_set_update.img_bg){
        lv_obj_del(ui_set_update.img_bg);
        ui_set_update.img_bg = NULL;
        ui_set_update.img_esc = NULL;
    }
    if(ui_set_update.title){
        lv_obj_del(ui_set_update.title);
        ui_set_update.title = NULL;
    }
    if(ui_set_update.version){
        lv_obj_del(ui_set_update.version);
        ui_set_update.version = NULL;
    }
    if(ui_set_update.bg_txt){
        lv_obj_del(ui_set_update.bg_txt);
        ui_set_update.bg_txt = NULL;
        ui_set_update.txt = NULL;
    }
    if(ui_set_update.bg_update){
        lv_obj_del(ui_set_update.bg_update);
        ui_set_update.bg_update = NULL;
    }
    if(ui_set_update.txt_update){
        lv_obj_del(ui_set_update.txt_update);
        ui_set_update.txt_update = NULL;
    }

    if(ui_set_update.txt_show){
        free(ui_set_update.txt_show);
        ui_set_update.txt_show = NULL;
    }
 
    return 0;
}


static void setting_update_on_view_did_load(page_base_t* self)
{
    ui_setting_update_create(self->root);
}

static void setting_update_on_view_did_unload(page_base_t* self)
{
    ui_setting_update_destroy(self->root);
}

static void setting_update_on_custom_attr_config(page_base_t* self)
{}
static void setting_update_on_view_load(page_base_t* self)
{}
static void setting_update_on_view_will_appear(page_base_t* self)
{}
static void setting_update_on_view_did_appear(page_base_t* self)
{}
static void setting_update_on_view_will_disappear(page_base_t* self)
{}
static void setting_update_on_view_did_disappear(page_base_t* self)
{}

static page_vtable_t setting_update_param = {
    .on_custom_attr_config  = setting_update_on_custom_attr_config,
    .on_view_load           = setting_update_on_view_load,
    .on_view_did_load       = setting_update_on_view_did_load,
    .on_view_will_appear    = setting_update_on_view_will_appear,
    .on_view_did_appear     = setting_update_on_view_did_appear,
    .on_view_will_disappear = setting_update_on_view_will_disappear,
    .on_view_did_disappear  = setting_update_on_view_did_disappear,
    .on_view_did_unload     = setting_update_on_view_did_unload,
};


page_vtable_t* lv_page_setting_update_create(void)
{
    return &setting_update_param;
}
